package com.capgemini.mulefocus.transformer;

import java.util.Date;
import java.util.List;

import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;
import org.mule.transformer.types.DataTypeFactory;

import com.capgemini.mulefocus.data.MongoDBOutputCollection;
import com.capgemini.mulefocus.data.MongoDBOutputObject;
import com.mongodb.BasicDBObject;

public class MongoDBStringToEyeCandy extends AbstractTransformer{

	
	public MongoDBStringToEyeCandy() {
		super();
		 this.registerSourceType(DataTypeFactory.create(List.class));
	     this.setReturnDataType(DataTypeFactory.create(MongoDBOutputCollection.class));
	}

	@SuppressWarnings("unchecked")
	@Override
	protected Object doTransform(Object src, String enc)
			throws TransformerException {
		List<BasicDBObject> source = (List<BasicDBObject>) src;
		MongoDBOutputCollection output = new MongoDBOutputCollection();
		
		for(BasicDBObject obj : source){
			MongoDBOutputObject row = new MongoDBOutputObject();
			row.setName((String) obj.get("name"));
			row.setGreeting((String) obj.get("greeting"));
			row.setTimestamp((Date)obj.get("timestamp"));
			output.appendRow(row.toString());
		}
		return output;
	}

}
